home *** CD-ROM | disk | FTP | other *** search
- program Expert;
- {$APPTYPE CONSOLE}
- uses
- Facts, Rules, Controls, Dialogs;
-
- function Forwards: Integer;
- var
- RulesFired,i: Integer;
- begin
- Result := 0;
- RulesFired := NumRule;
- _Fact[5].Value := Yes; { start }
- while (Result = 0) and (RulesFired > 0) do
- begin
- RulesFired := 0;
- for i:=1 to RuleMax do { all rules }
- begin
- if TestRule(i) then
- begin
- FireRule(i);
- Inc(RulesFired)
- end
- end;
- Result := NumFact;
- while (Result > 0) and
- ((not _Fact[Result].Goal) or
- ((_Fact[Result].Goal) and
- (_Fact[Result].Value = UnKnown))) do Dec(Result)
- end
- end {Forwards};
-
- procedure Backwards(Goal: Integer);
- Const Depth: Word = 0;
- var i,j: Integer;
- begin
- Inc(Depth);
- writeln(' ':Depth,Goal);
- i := 1;
- while i <= RuleMax do { all rules }
- begin
- if Conclude(i,Goal) then
- begin
- if TestRule(i) then FireRule(i)
- else { infer or ask }
- begin
- j := 1;
- while j <= NumRule do
- begin
- if (_Rule[j].Rule = i) and (_Rule[j].CF = 0) and
- (_Fact[_Rule[j].Fact].Value = UnKnown) then
- begin
- Backwards(_Rule[j].Fact); { infer }
- if TestRule(i) then j := NumRule
- else { ask }
- begin
- if _Fact[_Rule[j].Fact].Question <> '' then
- begin
- writeln(' ':Depth,_Fact[_Rule[j].Fact].Question);
- if MessageDlg(_Fact[_Rule[j].Fact].Question,
- mtConfirmation,[mbYes,mbNo],0) = mrYes then
- _Fact[_Rule[j].Fact].Value := Yes
- else
- begin
- _Fact[_Rule[j].Fact].Value := No; { can never prove }
- j := NumRule
- end
- end;
- if TestRule(i) then j := NumRule
- end
- end;
- Inc(j)
- end;
- if TestRule(i) then
- begin
- FireRule(i);
- i := RuleMax
- end;
- end
- end;
- Inc(i)
- end;
- Dec(Depth)
- end {Backwards};
-
- var
- Goal: Integer;
- begin
- writeln(NumFact,': facts');
- writeln(NumRule,': rules');
- Goal := Forwards;
- if (Goal > 0) and _Fact[Goal].Goal and (_Fact[Goal].Value <> UnKnown) then
- begin
- writeln('Forward chaining: ');
- writeln(_Fact[Goal].Name);
- writeln(ValueStr[_Fact[Goal].Value])
- end;
- writeln;
- for Goal:=1 to NumFact do _Fact[Goal].Value := UnKnown;
- for Goal:=1 to NumRule do _Rule[Goal].Fired := False;
- Goal := 1;
- Backwards(Goal);
- writeln('Backwards chaining: ');
- writeln(_Fact[Goal].Name);
- writeln(ValueStr[_Fact[Goal].Value]);
- for Goal:=2 to NumFact do writeln(_Fact[Goal].Name,' => ',ValueStr[_Fact[Goal].Value])
- end.